package com.moody.hibernate.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.CollateralInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * CollateralInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.CollateralInfo
 * @author MyEclipse Persistence Tools
 */

public class CollateralInfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CollateralInfoDAO.class);
	// property constants
	public static final String COLLATERAL_NAME = "collateralName";
	public static final String COLLATERAL_TYPE = "collateralType";
	public static final String COLLATERAL_TERM = "collateralTerm";
	public static final String NOW_VALUE = "nowValue";
	public static final String LOAN_VALUE = "loanValue";
	public static final String LOAN_VALUE_DATE = "loanValueDate";
	public static final String DEAL_VALUE = "dealValue";
	public static final String DEAL_VALUE_DATE = "dealValueDate";
	public static final String RECOVER_DATE = "recoverDate";
	public static final String BANK_SHARE = "bankShare";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";

	protected void initDao() {
		// do nothing
	}

	public void save(CollateralInfo transientInstance) {
		log.debug("saving CollateralInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CollateralInfo persistentInstance) {
		log.debug("deleting CollateralInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CollateralInfo findById(java.lang.String id) {
		log.debug("getting CollateralInfo instance with id: " + id);
		try {
			CollateralInfo instance = (CollateralInfo) getHibernateTemplate()
					.get("com.moody.hibernate.domain.CollateralInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CollateralInfo instance) {
		log.debug("finding CollateralInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CollateralInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CollateralInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCollateralName(Object collateralName) {
		return findByProperty(COLLATERAL_NAME, collateralName);
	}

	public List findByCollateralType(Object collateralType) {
		return findByProperty(COLLATERAL_TYPE, collateralType);
	}

	public List findByCollateralTerm(Object collateralTerm) {
		return findByProperty(COLLATERAL_TERM, collateralTerm);
	}

	public List findByNowValue(Object nowValue) {
		return findByProperty(NOW_VALUE, nowValue);
	}

	public List findByLoanValue(Object loanValue) {
		return findByProperty(LOAN_VALUE, loanValue);
	}

	public List findByLoanValueDate(Object loanValueDate) {
		return findByProperty(LOAN_VALUE_DATE, loanValueDate);
	}

	public List findByDealValue(Object dealValue) {
		return findByProperty(DEAL_VALUE, dealValue);
	}

	public List findByDealValueDate(Object dealValueDate) {
		return findByProperty(DEAL_VALUE_DATE, dealValueDate);
	}

	public List findByRecoverDate(Object recoverDate) {
		return findByProperty(RECOVER_DATE, recoverDate);
	}

	public List findByBankShare(Object bankShare) {
		return findByProperty(BANK_SHARE, bankShare);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findByUpdateUserId(Object updateUserId) {
		return findByProperty(UPDATE_USER_ID, updateUserId);
	}

	public List findByUpdateOrgId(Object updateOrgId) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId);
	}

	public List findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findAll() {
		log.debug("finding all CollateralInfo instances");
		try {
			String queryString = "from CollateralInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CollateralInfo merge(CollateralInfo detachedInstance) {
		log.debug("merging CollateralInfo instance");
		try {
			CollateralInfo result = (CollateralInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CollateralInfo instance) {
		log.debug("attaching dirty CollateralInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CollateralInfo instance) {
		log.debug("attaching clean CollateralInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CollateralInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CollateralInfoDAO) ctx.getBean("CollateralInfoDAO");
	}

	public void update(CollateralInfo transientInstance) {
		log.debug("updating CollateralInfo instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void deleteById(String collateralId) {
		String hql = "delete from CollateralInfo where collateralId = ?";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		query.setParameter(0, collateralId);
		if(query.executeUpdate() > 0)
			log.info("删除成功");
		else
			log.info("删除0个元素");
		session.close();
		
	}

	public List<CollateralInfo> findCustByCondition(
			Map<String, String> condition) {
		String hql = "FROM CollateralInfo WHERE ";
		Set<String> keySet = condition.keySet();
		Iterator<String> it = keySet.iterator();
		while(it.hasNext()){
			String keyString = it.next();
			hql += keyString + " like :" + keyString + " AND ";
		}
		hql = hql.substring(0,hql.length() - 5);
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		it = keySet.iterator();
		while(it.hasNext()){
			String keyString = it.next();
			query.setString(keyString, "%" + condition.get(keyString) +"%");
		}
		List<CollateralInfo> resultList = query.list();
		session.close();
		return resultList;
	}
}